<?php

	function test()
 	{
	 	//使用方法-------------------------------------------------
	 	$data = [
	 	    'photo' => 'bg.jpg',
	 	    'bg' => 'bg2.jpg',
	 	    'title' =>'这是一个标题',
	 	    'title_' =>'这是一个副标题',
	 	    'price' => 19.8,
	 	    'original_price' => 119.8,
	 	    'time' => '12.10-12.30',
	 	    'qrcode' => 'qrcode.png',
	 	    'logo' => 'jxl.png',
	 	];
	 	//直接输出
	 	// createSharePng($data);
	 	//输出到图片
	 	createSharePng($data,'share.png');
 	}
	/**
	 * 分享图片生成
	 * @param $data  商品数据，array
	 * @param $fileName string 保存文件名,默认空则直接输入图片
	 */
	function createSharePng($data,$fileName = ''){
	    //创建画布
	    $im = imagecreatetruecolor(750, 1334);
	 
	    //填充画布背景色
	    $color = imagecolorallocate($im, 255, 255, 255);
	    imagefill($im, 0, 0, $color);
	 
	    //字体文件
	    $font_file = "msyh.ttf";
	    $font_file_bold = "msyhbd.ttf";
	 
	    //设定字体的颜色
	    $font_color_1 = ImageColorAllocate ($im, 255, 168, 98);
	    $font_color_2 = ImageColorAllocate ($im, 153, 153, 153);
	    $font_color_3 = ImageColorAllocate ($im, 51, 51, 51);
	    $font_color_4 = ImageColorAllocate ($im, 255, 255, 255);
	    $font_color_5 = ImageColorAllocate ($im, 255, 98, 98);
	 
	    // 海报背景图
	    list($g_w,$g_h) = getimagesize($data['photo']);
	    $goodImg = createImageFromFile($data['photo']);
	    imagecopyresized($im, $goodImg, 0, 0, 0, 0, 750, 1334, $g_w, $g_h);
	    // logo
	    list($l_w,$l_h) = getimagesize($data['logo']);
	    $logoImg = imagecreatefrompng($data['logo']);
	    imagecopyresized($im, $logoImg, 30, 100, 0, 0, 100, 100, $l_w, $l_h);
	    // bg2
	    list($l_w,$l_h) = getimagesize($data['bg']);
	    $logoImg = createImageFromFile($data['bg']);
	    imagecopyresized($im, $logoImg, 30, 950, 0, 0, 690, 280, $l_w, $l_h);
	 
	    //二维码
	    list($code_w,$code_h) = getimagesize($data['qrcode']);
	    $codeImg = createImageFromFile($data['qrcode']);
	    imagecopyresized($im, $codeImg, 60, 980, 0, 0, 180, 180, $code_w, $code_h);
	 
	    //文字
	    // $theTitle = cn_row_substr($data['title'],2,19);
	    $fontBox = imagettfbbox(50, 0, $font_file, $data['title']);//文字水平居中实质
	    imagettftext($im, 50,0, ceil((750 - $fontBox[2]) / 2), 100, $font_color_4 ,$font_file, $data['title']);
	    imagettftext($im, 50,0, ceil((750 - $fontBox[2]) / 2)-50, 100, $font_color_4 ,$font_file, '-');
	    imagettftext($im, 50,0, ceil((750 - $fontBox[2]) / 2)+$fontBox[2]+20, 100, $font_color_4 ,$font_file, '-');
	    $fontBox = imagettfbbox(46, 0, $font_file, $data['title_']);//文字水平居中实质
	    imagettftext($im, 46,0, ceil((750 - $fontBox[2]) / 2), 200, $font_color_3 ,$font_file, $data['title_']);
	 
	    imagettftext($im, 26,0, 281, 1056, $font_color_1 ,$font_file, $data["time"]);
	    imagettftext($im, 22,0, 281, 1119, $font_color_2 ,$font_file, '原价'.$data["price"].'元');
	    imagettftext($im, 30,0, 285,1168, $font_color_3 ,$font_file, "秒杀价");
	    imagettftext($im, 40,0, 419,1170, $font_color_5 ,$font_file_bold, $data["original_price"].'元');
	    imagettftext($im, 22,0, 30,1272, $font_color_4 ,$font_file, "咨询电话");
	    imagettftext($im, 22,0, 30,1310, $font_color_4 ,$font_file, "地址");
	    //输出图片
	    if($fileName){
	        imagepng ($im,$fileName);
	    }else{
	    	header("Content-type:text/html;charset=utf-8");
	        Header("Content-Type: image/png");
	        imagepng ($im);
	    }
	 
	    //释放空间
	    imagedestroy($im);
	    imagedestroy($goodImg);
	    imagedestroy($codeImg);
	}
	 
	/**
	 * 从图片文件创建Image资源
	 * @param $file 图片文件，支持url
	 * @return bool|resource    成功返回图片image资源，失败返回false
	 */
	function createImageFromFile($file){
	    if(preg_match('/http(s)?:\/\//',$file)){
	        $fileSuffix = getNetworkImgType($file);
	    }else{
	    	$file_info = getimagesize($file);
	    	$fileSuffix = $file_info[2];
	        // $fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
	    }

	    if(!$fileSuffix) return false;
	 
	    switch ($fileSuffix){
	        /*case 'jpeg':
	            $theImage = @imagecreatefromjpeg($file);
	            break;*/
	        case '2':
	            $theImage = imagecreatefromjpeg($file);
	            break;
	        case '3':
	            $theImage = @imagecreatefrompng($file);
	            break;
	        case '1':
	            $theImage = @imagecreatefromgif($file);
	            break;
	        default:
	            $theImage = @imagecreatefromstring(file_get_contents($file));
	            break;
	    }
	 
	    return $theImage;
	}
	 
	/**
	 * 获取网络图片类型
	 * @param $url  网络图片url,支持不带后缀名url
	 * @return bool
	 */
	function getNetworkImgType($url){
	    $ch = curl_init(); //初始化curl
	    curl_setopt($ch, CURLOPT_URL, $url); //设置需要获取的URL
	    curl_setopt($ch, CURLOPT_NOBODY, 1);
	    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);//设置超时
	    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //支持https
	    curl_exec($ch);//执行curl会话
	    $http_code = curl_getinfo($ch);//获取curl连接资源句柄信息
	    curl_close($ch);//关闭资源连接
	 
	    if ($http_code['http_code'] == 200) {
	        $theImgType = explode('/',$http_code['content_type']);
	 
	        if($theImgType[0] == 'image'){
	            return $theImgType[1];
	        }else{
	            return false;
	        }
	    }else{
	        return false;
	    }
	}
	 
	/**
	 * 分行连续截取字符串
	 * @param $str  需要截取的字符串,UTF-8
	 * @param int $row  截取的行数
	 * @param int $number   每行截取的字数，中文长度
	 * @param bool $suffix  最后行是否添加‘...’后缀
	 * @return array    返回数组共$row个元素，下标1到$row
	 */
	function cn_row_substr($str,$row = 1,$number = 10,$suffix = true){
	    $result = array();
	    for ($r=1;$r<=$row;$r++){
	        $result[$r] = '';
	    }
	 
	    $str = trim($str);
	    if(!$str) return $result;
	 
	    $theStrlen = strlen($str);
	 
	    //每行实际字节长度
	    $oneRowNum = $number * 3;
	    for($r=1;$r<=$row;$r++){
	        if($r == $row and $theStrlen > $r * $oneRowNum and $suffix){
	            $result[$r] = mg_cn_substr($str,$oneRowNum-6,($r-1)* $oneRowNum).'...';
	        }else{
	            $result[$r] = mg_cn_substr($str,$oneRowNum,($r-1)* $oneRowNum);
	        }
	        if($theStrlen < $r * $oneRowNum) break;
	    }
	 
	    return $result;
	}
	 
	/**
	 * 按字节截取utf-8字符串
	 * 识别汉字全角符号，全角中文3个字节，半角英文1个字节
	 * @param $str  需要切取的字符串
	 * @param $len  截取长度[字节]
	 * @param int $start    截取开始位置，默认0
	 * @return string
	 */
	function mg_cn_substr($str,$len,$start = 0){
	    $q_str = '';
	    $q_strlen = ($start + $len)>strlen($str) ? strlen($str) : ($start + $len);
	 
	    //如果start不为起始位置，若起始位置为乱码就按照UTF-8编码获取新start
	    if($start and json_encode(substr($str,$start,1)) === false){
	        for($a=0;$a<3;$a++){
	            $new_start = $start + $a;
	            $m_str = substr($str,$new_start,3);
	            if(json_encode($m_str) !== false) {
	                $start = $new_start;
	                break;
	            }
	        }
	    }
	 
	    //切取内容
	    for($i=$start;$i<$q_strlen;$i++){
	        //ord()函数取得substr()的第一个字符的ASCII码，如果大于0xa0的话则是中文字符
	        if(ord(substr($str,$i,1))>0xa0){
	            $q_str .= substr($str,$i,3);
	            $i+=2;
	        }else{
	            $q_str .= substr($str,$i,1);
	        }
	    }
	    return $q_str;
	}

	test();